package JZOffer;

import java.util.*;

// https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/?envType=study-plan&id=lcof&plan=lcof&plan_progress=xxj1ozje
// 从尾到头打印链表
public class Day002_1 {
    public static int[] reversePrint(ListNode head) {
        // 辅助栈法
        Deque<Integer> stack = new ArrayDeque<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        int[] arr = new int[stack.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = stack.pop();
        }
        return arr;
    }

    public static void main(String[] args) {
        MySingleList list = new MySingleList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        int[] arr = reversePrint(list.head);
        for (int x : arr) {
            System.out.println(x);
        }
    }
}
